﻿using AbstructFactory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using XF.Common;
using XF.Meeting.Api.Common;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.Query;

namespace XF.Meeting.Api.WebServices
{
    /// <summary>
    /// MeetWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "XF.Meeting")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    [System.Web.Script.Services.ScriptService]
    public class MeetWebService : System.Web.Services.WebService
    {
        private UEntity.LogEntity.ELogRecordEntity LogEntity = new UEntity.LogEntity.ELogRecordEntity();
        public AbsFactoryTE abs = (AbsFactoryTE)AbsFactory.CreateInterface();
        [WebMethod]
        public string CreateResult(string strBTID, string strBOID, string bSuccess, int iProcInstID, string procURL, string strMessage)
        {
            LogEntity.Module = "XF.Meeting.Service.BPMService.CreateResult";
            LogEntity.OperateObject = "CreateResult";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                if (bSuccess == "1")
                {
                    var service = abs.CreateBPMInterface("审批创建结果", CommonIPHelper.GetIPAddress);
                    if (string.IsNullOrEmpty(strBTID))
                    {
                        strBTID = "strBTID为空";
                    }
                    if (iProcInstID == 0)
                    {
                        strBTID = "iProcInstID为0";
                    }
                    if (string.IsNullOrEmpty(procURL))
                    {
                        strBTID = "procURL为空";
                    }

                    RecordLog();
                    var result = service.CreateResult(strBTID, strBOID, iProcInstID, bSuccess, procURL, strMessage);
                    return result.Result;
                }
                else
                {
                    RecordLog();
                    return "<success>成功</ success>";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                LogEntity.ErrorDescription = string.Format("CreateResult Exception:{0}", ex.ToString());
                RecordLog();
                return "<success>失败</ success>";
            }
        }
        [WebMethod]
        public string Audit(string strBTID, string strBOID, int iProcInstID, string strStepName, string strApproverId, string eAction, string strComment, System.DateTime dtTime)
        {
            LogEntity.Module = "XF.Meeting.Service.BPMService.Audit";
            LogEntity.OperateObject = "Audit";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = strBTID + "--" + strBOID + "--" + iProcInstID + "--" + strStepName + "--" + strApproverId + "--" + eAction + "==" + strComment + "--" + dtTime; ;
            try
            {
                if (string.IsNullOrWhiteSpace(strBTID))
                {
                    return "<error>strBTID为空</error>";
                }
                if (string.IsNullOrWhiteSpace(strBOID))
                {
                    return "<error>strBOID为空</error>";
                }
                //if (iProcInstID == 0)
                //{
                //    return "<error>iProcInstID为0</error>";
                //}
                if (string.IsNullOrWhiteSpace(strStepName))
                {
                    return "<error>strStepName为空</error>";
                }
                if (string.IsNullOrWhiteSpace(strApproverId))
                {
                    return "<error>strApproverId为空</error>";
                }
                if (string.IsNullOrWhiteSpace(eAction))
                {
                    return "<error>eAction为空</error>";
                }
                if (string.IsNullOrWhiteSpace(strComment))
                {
                    return "<error>strComment为空</error>";
                }
                if (dtTime == null)
                {
                    return "<error>dtTime为空</error>";
                }
                var service = abs.CreateBPMInterface("流程审批过程中", CommonIPHelper.GetIPAddress);
                var result = service.Audit(strBTID, strBOID, iProcInstID, strStepName, strApproverId, eAction, strComment, dtTime).Result;
                RecordLog();
                return result;
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                LogEntity.ErrorDescription = string.Format("Audit Exception:{0}", ex.ToString());
                RecordLog();
                return null;
            }
        }

        [WebMethod]
        public string ApproveClose(string strBTID, string strBOID, int iProcInstID, string eProcessInstanceResult, string strComment, DateTime dtTime)
        {
            LogEntity.Module = "XF.Meeting.Service.BPMService.ApproveClose";
            LogEntity.OperateObject = "ApproveClose";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var service = abs.CreateBPMInterface("审批结束", CommonIPHelper.GetIPAddress);
                var bResult = service.ApproveClose(strBTID, strBOID, iProcInstID, eProcessInstanceResult, strComment, dtTime).Result;
                if (eProcessInstanceResult == "1" && strBTID == "DY_MeetManage_Reserve")
                {

                    RecordLog();
                    var wx = new WeChatHelper();

                    wx.SendWxMssage(new CRoomPadQuery() { strBOID = strBOID, type = 2 }, false);
                    
                    try
                    {
                        wx.SendTPwdToCreator(new CRoomPadQuery() { strBOID = strBOID, type = 2 }, false);
                    }
                    catch (Exception ex)
                    {

                        LogEntity.OperateResult = "Fail";
                        LogEntity.ErrorDescription = string.Format("ApproveClose Exception:{0}", ex.ToString());
                        RecordLog();
                    }
                    var roomservice = abs.CreateConferenceRoomInterface("", CommonIPHelper.GetIPAddress);
                    var (res, strError, idd) = roomservice.InsertCInfoToOrgcalendarsForBPM(strBOID);
                    if (res && (idd > 0))
                    {
                        wx.InsertWechatSchedule(idd);
                    }
                   
                }

                return "<success>成功<success>";
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                LogEntity.ErrorDescription = string.Format("ApproveClose Exception:{0}", ex.ToString());
                RecordLog();
                return "<success>失败<success>";
            }
        }
        private void RecordLog()
        {
            LogEntity.Operator = "";
            LogEntity.OperateIP = "";
            ULogWork.CLogToDataBase.RecordOperateLog(LogEntity);
        }
    }
}
